package com.gd.iot.energy.service.dailymanage.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gd.iot.energy.code.ResponseCode;
import com.gd.iot.energy.consts.SystemConsts;
import com.gd.iot.energy.dao.dailymanage.EquipmentInstructionTreePOMapper;
import com.gd.iot.energy.exception.MyException;
import com.gd.iot.energy.po.dailymanage.EquipmentInstructionTreePO;
import com.gd.iot.energy.pojo.common.vo.ColumnVO;
import com.gd.iot.energy.pojo.common.vo.PageVO;
import com.gd.iot.energy.pojo.dailymanage.dto.EquipmentInstructionTreeSaveDTO;
import com.gd.iot.energy.pojo.dailymanage.vo.EquipmentInstructionTreePageVO;
import com.gd.iot.energy.pojo.dailymanage.vo.EquipmentInstructionTreeVO;
import com.gd.iot.energy.pojo.systemadmin.vo.EnumVO;
import com.gd.iot.energy.service.dailymanage.EquipmentInstructionTreeService;
import com.gd.iot.energy.util.CustomBeanUtils;
import com.gd.iot.energy.util.CustomColumnUtil;
import com.gd.iot.energy.util.CustomStrUtil;
import com.gd.iot.energy.util.EnergyTypeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class EquipmentInstructionTreeServiceImpl extends ServiceImpl<EquipmentInstructionTreePOMapper, EquipmentInstructionTreePO> implements EquipmentInstructionTreeService {
    @Resource
    private EquipmentInstructionTreePOMapper instructionTreePOMapper;

    /**
     * 表头文件
     */
    private static final String INSTRUCTION_LIST_COLUMN_FILE_NAME = "dailymanage/equipment_instruction_list.column";

    @Override
    public List<EquipmentInstructionTreeVO> getEquipmentInstructionTreeVOs(Integer equipmentId, String treeName) {
        QueryWrapper<EquipmentInstructionTreePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("equipment_id", equipmentId);
        queryWrapper.eq("is_del", SystemConsts.BooleanEnum.FALSE.value);
        if (StrUtil.isNotBlank(treeName)) {
            queryWrapper.like("tree_name", treeName);
        }
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        List<EquipmentInstructionTreePO> instructionTreePOS = instructionTreePOMapper.selectList(queryWrapper);
        List<EquipmentInstructionTreeVO> instructionTreeVOS = new ArrayList<>(instructionTreePOS.size());
        CustomBeanUtils.convertPojo(instructionTreePOS,instructionTreeVOS,EquipmentInstructionTreeVO.class);
        return instructionTreeVOS;
    }

    @Override
    public PageVO<EquipmentInstructionTreePageVO> getEquipmentInstructionTreePageVOs(Integer equipmentId, String treeName, Integer pageSize,
                                                                                     Integer pageNo, String orderColumn, String orderType) {
        IPage<EquipmentInstructionTreePO> page = new Page<>(pageNo, pageSize);
        QueryWrapper<EquipmentInstructionTreePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("equipment_id", equipmentId);
        queryWrapper.eq("is_del", SystemConsts.BooleanEnum.FALSE.value);
        if (StrUtil.isNotBlank(treeName)) {
            queryWrapper.like("tree_name", treeName.trim());
        }
        if (StrUtil.isNotBlank(orderColumn) && StrUtil.isNotBlank(orderType)) {
            orderColumn = CustomStrUtil.humpToLine(orderColumn);
            queryWrapper.orderBy(true, orderType.equals(SystemConsts.OrderType.ASC.value), orderColumn);
        }
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        IPage<EquipmentInstructionTreePO> instructionTreePOIPage = instructionTreePOMapper.selectPage(page, queryWrapper);
        PageVO<EquipmentInstructionTreePageVO> pageVO = new PageVO<>();
        pageVO.setPageSize(pageSize);
        pageVO.setPageNum(pageNo);
        List<EquipmentInstructionTreePageVO> treePageVOS = convertToEquipmentInstructionTreePageVOList(instructionTreePOIPage.getRecords(), pageSize, pageNo);
        pageVO.setData(treePageVOS);
        pageVO.setTotal(instructionTreePOIPage.getTotal());
        pageVO.setPages(instructionTreePOIPage.getPages());

        //表头
        List<ColumnVO> columns = CustomColumnUtil.getColumn(INSTRUCTION_LIST_COLUMN_FILE_NAME);
        pageVO.setColumns(columns);
        return pageVO;
    }

    /**
     * 对象转换
     *
     * @param records
     * @return
     */
    private List<EquipmentInstructionTreePageVO> convertToEquipmentInstructionTreePageVOList(List<EquipmentInstructionTreePO> records, Integer pageSize, Integer pageNo) {
        List<EquipmentInstructionTreePageVO> res = new ArrayList<>(records.size());
        EquipmentInstructionTreePageVO instructionTreePageVO;

        int i = pageSize * (pageNo - 1) + 1;
        for (EquipmentInstructionTreePO record : records) {
            instructionTreePageVO = new EquipmentInstructionTreePageVO();
            instructionTreePageVO.setSerialNum(i);
            instructionTreePageVO.setId(record.getId());
            instructionTreePageVO.setTreeName(record.getTreeName());
            instructionTreePageVO.setTreeValue(record.getTreeValue());
            instructionTreePageVO.setTreeAction(record.getTreeAction());
            instructionTreePageVO.setTreeMethod(record.getTreeMethod());
            instructionTreePageVO.setEquipmentTypeName(SystemConsts.EquipmentType.getDescByValue(record.getEquipmentType()));
            res.add(instructionTreePageVO);
            i++;
        }
        return res;
    }

    @Override
    public Long add(EquipmentInstructionTreeSaveDTO instructionTreeSaveDTO) {
        if (instructionTreeSaveDTO.getEquipmentId() == null ||
                StrUtil.isBlank(instructionTreeSaveDTO.getTreeName())) {
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        EquipmentInstructionTreePO instructionTreePO = new EquipmentInstructionTreePO();
        instructionTreePO.setCompanyId(0L);
        instructionTreePO.setCreateDate(new Date());
        instructionTreePO.setEnergyType(SystemConsts.EnergyTypeEnum.WATER.value);
        instructionTreePO.setIsDel(SystemConsts.BooleanEnum.FALSE.value);
        instructionTreePO.setModifyDate(new Date());
        instructionTreePO.setOperatorId(1L);

        instructionTreePO.setTreeAction(instructionTreeSaveDTO.getTreeAction());
        instructionTreePO.setTreeMethod(instructionTreeSaveDTO.getTreeMethod());
        instructionTreePO.setTreeName(instructionTreeSaveDTO.getTreeName());
        instructionTreePO.setEquipmentType(instructionTreeSaveDTO.getEquipmentType());
        instructionTreePO.setEquipmentId(instructionTreeSaveDTO.getEquipmentId());
        instructionTreePO.setSerialnumber(instructionTreeSaveDTO.getSerialnumber());
        instructionTreePO.setCommandType(instructionTreeSaveDTO.getCommandType());
        instructionTreePO.setCommandurl(instructionTreeSaveDTO.getCommandurl());
        instructionTreePO.setParseReadAction(instructionTreeSaveDTO.getParseReadAction());
        instructionTreePO.setParseReadMethod(instructionTreeSaveDTO.getParseReadMethod());
//        instructionTreePO.setTreeValue();
//        instructionTreePO.setControlCode();
//        instructionTreePO.setControlLogo();
//        instructionTreePO.setRecControlCode();
//        instructionTreePO.setRecControlLogo();
//        instructionTreePO.setCommandName();
//        instructionTreePO.setServiceName();

        instructionTreePOMapper.insert(instructionTreePO);
        return instructionTreePO.getId();
    }

    @Override
    public boolean update(EquipmentInstructionTreeSaveDTO instructionTreeSaveDTO) {
        if (instructionTreeSaveDTO.getEquipmentId() == null ||
                StrUtil.isBlank(instructionTreeSaveDTO.getTreeName())) {
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        EquipmentInstructionTreePO instructionTreePO = new EquipmentInstructionTreePO();
        instructionTreePO.setId(instructionTreeSaveDTO.getId());
        instructionTreePO.setModifyDate(new Date());
        instructionTreePO.setOperatorId(1L);

        instructionTreePO.setTreeAction(instructionTreeSaveDTO.getTreeAction());
        instructionTreePO.setTreeMethod(instructionTreeSaveDTO.getTreeMethod());
        instructionTreePO.setTreeName(instructionTreeSaveDTO.getTreeName());
        instructionTreePO.setEquipmentType(instructionTreeSaveDTO.getEquipmentType());
        instructionTreePO.setEquipmentId(instructionTreeSaveDTO.getEquipmentId());
        instructionTreePO.setSerialnumber(instructionTreeSaveDTO.getSerialnumber());
        instructionTreePO.setCommandType(instructionTreeSaveDTO.getCommandType());
        instructionTreePO.setCommandurl(instructionTreeSaveDTO.getCommandurl());
        instructionTreePO.setParseReadAction(instructionTreeSaveDTO.getParseReadAction());
        instructionTreePO.setParseReadMethod(instructionTreeSaveDTO.getParseReadMethod());
//        instructionTreePO.setTreeValue();
//        instructionTreePO.setControlCode();
//        instructionTreePO.setControlLogo();
//        instructionTreePO.setRecControlCode();
//        instructionTreePO.setRecControlLogo();
//        instructionTreePO.setCommandName();
//        instructionTreePO.setServiceName();

        instructionTreePOMapper.updateById(instructionTreePO);
        return true;
    }

    @Override
    public boolean delete(List<Integer> ids) {
        if(CollUtil.isEmpty(ids)){
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        //供应商表逻辑删除
        QueryWrapper<EquipmentInstructionTreePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        EquipmentInstructionTreePO metermodelPO = new EquipmentInstructionTreePO();
        metermodelPO.setIsDel(SystemConsts.BooleanEnum.TRUE.value);
        int num = instructionTreePOMapper.update(metermodelPO,queryWrapper);

        return num > 0;
    }

    @Override
    public EquipmentInstructionTreeSaveDTO getEquipmentInstructionTree(Integer id) {
        EquipmentInstructionTreePO instructionTreePO = instructionTreePOMapper.selectById(id);
        if(instructionTreePO==null||SystemConsts.BooleanEnum.TRUE.value.equals(instructionTreePO.getIsDel())){
            throw new MyException(ResponseCode.RECORD_NOT_EXIST);
        }

        EquipmentInstructionTreeSaveDTO instructionTreeSaveDTO = new EquipmentInstructionTreeSaveDTO();
        instructionTreeSaveDTO.setId(instructionTreePO.getId());
        instructionTreeSaveDTO.setTreeAction(instructionTreePO.getTreeAction());
        instructionTreeSaveDTO.setTreeMethod(instructionTreePO.getTreeMethod());
        instructionTreeSaveDTO.setTreeName(instructionTreePO.getTreeName());
        instructionTreeSaveDTO.setEquipmentType(instructionTreePO.getEquipmentType());
        instructionTreeSaveDTO.setEquipmentId(instructionTreePO.getEquipmentId());
        instructionTreeSaveDTO.setSerialnumber(instructionTreePO.getSerialnumber());
        instructionTreeSaveDTO.setCommandType(instructionTreePO.getCommandType());
        instructionTreeSaveDTO.setCommandurl(instructionTreePO.getCommandurl());
        instructionTreeSaveDTO.setParseReadAction(instructionTreePO.getParseReadAction());
        instructionTreeSaveDTO.setParseReadMethod(instructionTreePO.getParseReadMethod());

        return instructionTreeSaveDTO;
    }

    @Override
    public Map<String, List<EnumVO>> getEnums() {
        Map<String, List<EnumVO>> resultMap = new HashMap<>();

        List<EnumVO> equipmentType = new ArrayList<>();
        for (SystemConsts.EquipmentType value : SystemConsts.EquipmentType.values()) {
            EnumVO vo = new EnumVO();
            vo.setValue(value.value);
            vo.setValueName(value.valueName);
            vo.setDesc(value.desc);
            equipmentType.add(vo);
        }

        List<EnumVO> instructionType = new ArrayList<>();
        for (SystemConsts.EquipmentInstructionType value : SystemConsts.EquipmentInstructionType.values()) {
            EnumVO vo = new EnumVO();
            vo.setValue(value.value);
            vo.setValueName(value.valueName);
            vo.setDesc(value.desc);
            instructionType.add(vo);
        }

        resultMap.put("equipmentType", equipmentType);
        resultMap.put("instructionType", instructionType);
        return resultMap;
    }
}
